package com.may.wind.controller;

import com.may.wind.encrypt.annotation.Decrypt;
import com.may.wind.encrypt.annotation.Encrypt;
import com.may.wind.encrypt.util.RsaUtil;
import com.may.wind.entity.User;
import com.may.wind.pojo.Register;
import com.may.wind.pojo.Result;
import com.may.wind.service.UserService;
import com.may.wind.util.VerifyCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 风
 */
@Api(tags = "用户登录状态、注册等相关API集合")
@Controller
public class LoginController {
    @Autowired
    UserService userService;

    /**
     * 获取验证码
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "获取验证码", httpMethod = "GET")
    @GetMapping("/api/getVerifyCode")
    public void getVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        String code = verifyCode.getCode();

        // 将四位数字的验证码保存到Session中。
        HttpSession session = request.getSession();
        session.setAttribute("validateCode", code);

        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        // 将图像输出到Servlet输出流中
        ServletOutputStream stream = response.getOutputStream();
        ImageIO.write(image, "jpeg", stream);
        // 推送并关闭响应输出流
        try {
            stream.flush();
        } finally {
            stream.close();
        }
    }

    /**
     * 用户注册
     *
     * @param register
     * @return
     */
    @ApiOperation(value = "注册用户", httpMethod = "POST")
    @ApiImplicitParam(name = "register", value = "用户信息", required = true)
    @PostMapping(value = "/api/register")
    @ResponseBody
    @Decrypt
    @Encrypt
    public Result register(Register register) {
        userService.register(register);
        return Result.ok();
    }

    @ApiOperation(value = "用户登录", notes = "弃用", httpMethod = "POST")
    @ApiImplicitParam(name = "user", value = "用户信息", required = true)
    @PostMapping(value = "/api/login")
    @ResponseBody
    public Result login(User requestUser) {
        return Result.ok();
    }

    /**
     * 获取用户认证信息
     *
     * @param authentication
     * @return
     */
    @ApiOperation(value = "获取当前登录用户状态", notes = "获取用户登录状态时使用", httpMethod = "GET")
    @GetMapping("/api/getMe")
    @ResponseBody
    public Result getMe(Authentication authentication) {
        Object principal = null;
        if (authentication != null) {
            principal = authentication.getPrincipal();
        }
        return Result.ok().data("user", principal);
    }

    /**
     * 获取RSA公钥
     *
     * @return
     */
    @ApiOperation(value = "获取后台RSA公钥", notes = "在登录初始化时调用", httpMethod = "GET")
    @GetMapping(value = "/api/getPublicKey")
    @ResponseBody
    public Result<String> getPublicKey() {
        String publicKey = RsaUtil.getPublicKey();
        return Result.ok().data("publicKey", publicKey);
    }
}
